// ---------- 页面头部 ----------
//头部的划过显示效果
$('.buy').stop().hover(()=>{
    $('.buy ul').stop().show(300)
},()=>{
    $('.buy ul').stop().hide()
})
$('.customer').stop().hover(()=>{
    $('.customer ul').stop().show(300)
},()=>{
    $('.customer ul').stop().hide()
})
$('.APP').stop().hover(()=>{
    $('.APP ul').stop().show(300)
},()=>{
    $('.APP ul').stop().hide()
})

//导航栏吸顶效果

window.addEventListener("scroll",()=>{
	let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;  
    let height = document.querySelector('#searchBar').clientHeight;
    if (scrollTop > height+1) {
        $('.searchBox').addClass('disappear')
        $('.cart span').addClass('disappear')
        $('.nav li:nth-child(10)').addClass('disappear')
        $('.nav li:nth-child(11)').addClass('disappear')
        $('.nav li:nth-child(12)').addClass('disappear')
        $('.cart').addClass('cartTop')
        $('.search').addClass('searchTop')
        $('.nav').addClass('navTop')
        $('.logo').addClass('logoTop')
        $('.icon1').removeClass('disappear')
        $('.icon2').removeClass('disappear')
    } else {
        $('.searchBox').removeClass('disappear')
        $('.cart span').removeClass('disappear')
        $('.nav li:nth-child(10)').removeClass('disappear')
        $('.nav li:nth-child(11)').removeClass('disappear')
        $('.nav li:nth-child(12)').removeClass('disappear')
        $('.cart').removeClass('cartTop')
        $('.search').removeClass('searchTop')
        $('.nav').removeClass('navTop')
        $('.logo').removeClass('logoTop')
        $('.icon1').addClass('disappear')
        $('.icon2').addClass('disappear')
    }
})

// 返回顶部效果
$(".goTop").bind("click",function(){
    $('html, body').animate({scrollTop: 0},300);
})

//---------- 发送Ajax请求，获取信息 ----------
$.ajax({
    url:'/globalinfo',
    dataType: 'json',
    data: {
        '__timestamp': +new Date()
    },
    success: function (res){
        // console.log(res)
        //更改搜索栏提示文字
        let index = 0
        fn()
        setInterval(fn,3000)
        function fn(){
            $('.searchBox input').prop('placeholder',res.data.defaultWordsVos[index].content)
            index++
            if(index>=res.data.defaultWordsVos.length) index = 0
        }
        //给导航栏设置文字并跳转
        nav = res.data.cateList
        console.log(nav)
        let str = '<li><a href="./index.html" class="first">首页</a></li>'
        nav.forEach(item => {
            str += `
            <li><a href="./list.html">${item.name}</a>
                <ol>`
            item.subCateList.forEach(items=>{
                str +=`<li>
                <a href="./list.html"><img src="${items.bannerUrl}">${items.name}</a>
                </li>`
            })
            str +=`</ol>
            </li>`
        });
        str += `<li style="margin: 0;"></li>
                <li><a href="http://you.163.com/topic/index">为你严选</a></li>
                <li><a href="http://you.163.com/zc/project/list">众筹</a></li>`
        $('.nav').html(str)
        // 导航栏各项内容点击跳转到列表页
        $('.nav>li').click(function(){
            let index = $('.nav>li').index($(this))-1
            localStorage.setItem('categoryId',nav[index].id)
            localStorage.setItem('formName',nav[index].name)
        })
        // 导航分栏的点击跳转
        $('.nav ol li').click(function(){
            let index1 = $('.nav>li').index($(this).parent().parent())-1
            let index2 = $('.nav ol li').index($(this))
            localStorage.setItem('subCategoryId',nav[index1].subCateList[index2].id)
            localStorage.setItem('subCategoryName',nav[index1].subCateList[index2].name)
        })
        
    },
    error: function(){
        console.log('error')
    }
})

let id = localStorage.getItem('id')
if(id){
    document.querySelector('.first-li').innerHTML = id
}